package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.LoginLogService;
import com.xbongbong.paas.service.OperateLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.log.pojo.dto.FormSearchListDTO;
import com.xbongbong.pro.log.pojo.dto.LoginLogListDTO;
import com.xbongbong.pro.log.pojo.dto.OperateDetailLogDTO;
import com.xbongbong.pro.log.pojo.dto.OperateLogConditionDTO;
import com.xbongbong.pro.log.pojo.dto.OperateLogListDTO;
import com.xbongbong.pro.log.pojo.vo.FormSearchListVO;
import com.xbongbong.pro.log.pojo.vo.LoginLogListVO;
import com.xbongbong.pro.log.pojo.vo.OperateDetailLogVO;
import com.xbongbong.pro.log.pojo.vo.OperateLogConditionVO;
import com.xbongbong.pro.log.pojo.vo.OperateLogListVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 系统日志相关接口
 * @author kaka
 * @date 2019/1/21 15:52
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/log")
public class LogController {

    private static final Logger LOG = LoggerFactory.getLogger(LogController.class);

    @Resource
    private LoginLogService loginLogService;
    @Resource
    private OperateLogService operateLogService;

    /**
     * 登录日志列表接口
     */
    @RequestMapping(value = "/getFormSearchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFormSearchList(@RequestBody @Valid FormSearchListDTO formSearchListDTO, BindingResult br) throws Exception {
        XbbResponse<FormSearchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormSearchListVO formSearchListVO = operateLogService.getFormSearchList(formSearchListDTO);
                response = new XbbResponse<>(formSearchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 登录日志列表接口
     */
    @RequestMapping(value = "/loginLog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String loginLogList(@RequestBody @Valid LoginLogListDTO loginLogListDTO, BindingResult br) throws Exception {
        XbbResponse<LoginLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LoginLogListVO loginLogListVO = loginLogService.list(loginLogListDTO);
                response = new XbbResponse<>(loginLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取操作日志筛选条件
     */
    @RequestMapping(value = "/operateLog/getCondition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String operateLogGetCondition(@RequestBody @Valid OperateLogConditionDTO operateLogConditionDTO, BindingResult br) throws Exception {
        XbbResponse<OperateLogConditionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OperateLogConditionVO operateLogConditionVO = operateLogService.getCondition(operateLogConditionDTO);
                response = new XbbResponse<>(operateLogConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 操作日志列表接口
     */
    @RequestMapping(value = "/operateLog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String operateLogList(@RequestBody @Valid OperateLogListDTO operateLogListDTO, BindingResult br) throws Exception {
        XbbResponse<OperateLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OperateLogListVO operateLogListVO = operateLogService.list(operateLogListDTO);
                response = new XbbResponse<>(operateLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 操作日志详情页
     * @param operateDetailLogDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @date 2019/6/24 22:27
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/operateLog/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String operateLogDetail(@RequestBody @Valid OperateDetailLogDTO operateDetailLogDTO, BindingResult br) throws Exception {
        XbbResponse<OperateDetailLogVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OperateDetailLogVO operateDetailLogVO = operateLogService.detail(operateDetailLogDTO);
                response = new XbbResponse<>(operateDetailLogVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
